<?php
/*
 * Created on Feb 19, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
include('../includes/session.php');
include('../models/transaction_model.php');

class TransactionController{
	
	function TransactionController(){
		global $session;
		if(isset($_POST['transedit'])){
        	$this->transEdit();
     	}else if(isset($_POST['transadd'])){
        	$this->transAdd();
     	}else{       		
        	header("Location: ".$session->referrer);
       	}
	}
	
	function transEdit(){
		global $session, $form;
		
		$resultValidation = $this->checkValidation(
										$_POST['SelectedTrans'],
										$_POST['name'], 
										$_POST['sortNo'],
										null);
		
		/* Account edit successful */
	    if($resultValidation == 0){
	    	$_SESSION['edit'] = true;
			$_SESSION['editname'] = $_POST['name'];
	        header("Location: ".$session->referrer);
	    }
	    /* Error found with form */
	    else{
	        $_SESSION['value_array'] = $_POST;
	        $_SESSION['error_array'] = $form->getErrorArray();
	        header("Location: ".$session->referrer);
		}
	}
	
	function transAdd(){
		global $session, $form;
				
	    $resultValidation = $this->checkValidation(
										null,
										$_POST['name'], 
										$_POST['sortNo'],
										$_POST['createDate']);
	    	
		/* Add Successful */
		if($resultValidation == 0){
	    	$_SESSION['successname'] = $_POST['name'];
	        $_SESSION['success'] = true;
	        header("Location: ".$session->referrer);
	    }
	    /* Error found with form */
	    else if($resultValidation == 1){
	        $_SESSION['value_array'] = $_POST;
	        $_SESSION['error_array'] = $form->getErrorArray();
	        header("Location: ".$session->referrer);
	    }
	    /* Add attempt failed */
	    else if($resultValidation == 2){
	        $_SESSION['successname'] = $_POST['name'];
	        $_SESSION['success'] = false;
	        header("Location: ".$session->referrer);
		}
	}
	
	function checkValidation($id = null, $name, $sortno, $createDate){
		global $form, $transactionModel;
		/* Username error checking */
      	$field = "name";  //Use field name for username
	    if(!$name || strlen($name = trim($name)) == 0){
	    	$form->setError($field, prnMsg(_('Nhập tên sàn giao dịch'),'error'));
	    }
				
		if(!$sortno || strlen($sortno = trim($sortno)) == 0){
	    	$sortno = 0;
	    }
		
		/* Errors exist, have user correct them */
	    if($form->num_errors > 0){
	    	return 1;  //Errors with form
	    }/* No errors, add the new account to the */
	    else{
	    	if(!isset($id)){
		        return $transactionModel->addNewTrans($name, $sortno, $createDate);					
			}else{
				return $transactionModel->editTrans($id, $name, $sortno);
			}
	   	}
	}
};

/* Create transaction controller object */
$transactionController = new TransactionController;
?>
